/*=========================================================================
Elite College as Engines of Upward Mobility: Evidence from Colombia's Ser Pilo Paga
Authors: Juliana Londoño-Vélez, Catherine Rodriguez, Fabio Sánchez
and Luis Esteban Álvarez-Arango

Creation date: June 6th, 2025
--------------------------------------------------------------------------
Figures 6a and 6b
=========================================================================*/

use "${data}/data_RD", clear
keep if icfes_per==20142

glo cond_saber11 "eligible_sisben==1"
glo cond_sisben	"eligible_saber11==1"
glo lim_saber11	40
glo lim_sisben 20

cap drop mean_include*
cap drop cut_saber11 
cap drop cut_sisben
gegen cut_saber11=cut(running_saber11), at(-100(2)100)
gegen mean_include_saber11 = tag(cut_saber11) if eligible_sisben==1
gegen cut_sisben=cut(running_sisben), at(-40(1)40)
gegen mean_include_sisben = tag(cut_sisben) if eligible_saber11==1

rdrobust mw_9 running_saber11 if ${cond_saber11}, masspoints(off) 
local bw=e(h_l)
cap drop kw
gen kw = ((((`bw')-abs(running_saber11))/(`bw'))*(abs(running_saber11) < (`bw')))
cap drop bin
gegen bin =  mean(mw_9) if ${cond_saber11}, by(cut_saber11)
	
#delimit ;
twoway	(lfit mw_9 running_saber11 [pw=kw] if abs(running_saber11)<`bw' & ${cond_saber11} & running_saber11 < 0, sort lcolor(black)) 
		(lfit mw_9 running_saber11 [pw=kw] if abs(running_saber11)<`bw' & ${cond_saber11} & running_saber11 >= 0, sort lcolor(black)) 
		(scatter bin cut_saber11 if mean_include_saber11==1 & abs(running_saber11)<${lim_saber11}, mc(black) ms(O) msize(small)), 
		xline(0, lcolor(red) lpattern(dash)) xsize(8) xlab(-40(20)40) xtitle(Distance to SABER 11 cutoff) ylab(0(0.2)2) caption(Sample restricted to low-SES students., size(small)) legend(off) graphregion(color(white)) ytitle("")
;
#delimit cr
	
graph export "${outcomes}/Fig6A.pdf", replace 


***

use 	"${Intermediate}/Resultados_years_pila.dta", clear

drop 	cs_* ci_*
foreach r in 11 si{
gen 	cs_`r' = cc_`r'+(1.96*sec_`r')
gen 	ci_`r' = cc_`r'-(1.96*sec_`r')
} 
	
gen 	p=.
replace p=1 if regexm(nombre,"201304") 
replace p=2 if regexm(nombre,"201308") 
replace p=3 if regexm(nombre,"201312") 
replace p=4 if regexm(nombre,"201404") 
replace p=5 if regexm(nombre,"201408") 
replace p=6 if regexm(nombre,"201412") 
replace p=7 if regexm(nombre,"201504") 
replace p=8 if regexm(nombre,"201508") 
replace p=9 if regexm(nombre,"201512") 
replace p=10 if regexm(nombre,"201604") 
replace p=11 if regexm(nombre,"201608") 
replace p=12 if regexm(nombre,"201612")
replace p=13 if regexm(nombre,"201704") 
replace p=14 if regexm(nombre,"201708") 
replace p=15 if regexm(nombre,"201712")
replace p=16 if regexm(nombre,"201804") 
replace p=17 if regexm(nombre,"201808") 
replace p=18 if regexm(nombre,"201812")
replace p=19 if regexm(nombre,"201904") 
replace p=20 if regexm(nombre,"201908") 
replace p=21 if regexm(nombre,"201912")
replace p=22 if regexm(nombre,"202004") 
replace p=23 if regexm(nombre,"202008") 
replace p=24 if regexm(nombre,"202012")
replace p=25 if regexm(nombre,"202104") 
replace p=26 if regexm(nombre,"202108") 
replace p=27 if regexm(nombre,"202112")
replace p=28 if regexm(nombre,"202204") 
replace p=29 if regexm(nombre,"202208") 
replace p=30 if regexm(nombre,"202212")
replace p=31 if regexm(nombre,"202304") 
replace p=32 if regexm(nombre,"202308") 
replace p=33 if regexm(nombre,"202312")

#delimit ;
twoway 	(rcap cs_11 ci_11 p if regexm(nombre,"mw"), lcolor(black))
		(scatter cc_11 p if regexm(nombre,"mw"), ysc(r(0)) mcolor(black) msymbol(O))
		,ytitle(RD Coefficient and 95% CI) yline(0) ylab(-.1(.05).3)
		xtitle("Years after high school completion")
		xlabel( 2 "-1" 5 "0" 8 "1" 11 "2" 14 "3" 17 "4" 20 "5" 23 "6" 26 "7" 
		29 "8" 32 "9",labsize(medium))
		legend(off) xsize(8)
		caption(Sample restricted to low-SES individuals, size(small)) 
		graphregion(color(white)) 
;
#delimit cr
	
graph export "${outcomes}/Fig6B.pdf", replace 
